** Figure 5 **
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"
global Appendixresultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results\appendix"

* 1. Finalize sample & variables
use "$Section4/analysis_main.dta", clear

* check that each module has 10 deciles 
bysort product_module_code: gen dup=[_N]
keep if dup==10
drop dup

* generate identifiers for outliers (with RMS spending weights)
foreach i in RMS_avg_Dlog_realspending ///
RMS_feenstra_win ///
RMS_ltornqvist_price_index_win ///
RMS_lfullces_pi_win_common  ///
inst_raw inst_linageres inst_ageres inst_ageeducres ///
inst_ageeducstateres inst_allres inst_educres inst_stateres ///
inst_linageducres inst_linagestateres inst_linagerace inst_linagechild ///
inst_raceres inst_childrenres RMS_lces_price_index_win { 
gen `i'_o=1
sum `i' [aw=RMS_avg_spending], d
gen `i'_w=`i'
replace `i'_w=r(p95) if `i'_w>r(p95)
replace `i'_w=r(p5) if `i'_w<r(p5)
replace `i'_o=0 if inrange(`i',r(p5),r(p95))
}

* repeat with HMS spending weights (for robustness tables)
* generate identifiers for outliers (with RMS spending weights)
rename HMS_ltornqvist_price_index_win HMS_ltornqvist_pi_win
rename lfullces_pi_win_common HMS_lfullces_pi_win_common

foreach i in HMS_avg_Dlog_realspending ///
HMS_ltornqvist_pi_win ///
HMS_lfullces_pi_win_common  ///
inst_raw inst_linageres inst_ageres inst_ageeducres ///
inst_ageeducstateres inst_allres inst_educres inst_stateres ///
inst_linageducres inst_linagestateres inst_linagerace inst_linagechild ///
inst_raceres inst_childrenres { 
gen `i'_HMSo=1
sum `i' [aw=HMS_avg_spending], d
gen `i'_HMSw=`i'
replace `i'_HMSw=r(p95) if `i'_HMSw>r(p95)
replace `i'_HMSw=r(p5) if `i'_HMSw<r(p5)
replace `i'_HMSo=0 if inrange(`i',r(p5),r(p95))
}

save "$Section4/analysis_main.dta", replace

* 2. Produce figures (with regression coefficients) 

* Main results with RMS data

use "$Section4/analysis_main.dta", clear

* rescale variables to make them easier to read
foreach i in RMS_avg_Dlog_realspending inst_linageres RMS_ltornqvist_price_index_win ///
RMS_lfullces_pi_win_common RMS_lces_price_index_win {
replace `i'=`i'*100
}

* Panel A
reg RMS_avg_Dlog_realspending inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter RMS_avg_Dlog_realspending inst_linageres [aw=sqrt(RMS_avg_spending)], nq(100) ///
xtitle("Instrument for Annualized Growth of Market Size (%)") ylabel(,angle(0)) ///
ytitle ("Annualized Growth of Market Size, 2006-2015 (%)") ///
graphregion(color(white)) note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure5a.pdf", as(pdf) replace
graph save "$resultspath/Figure5a.gph", replace 

* Panel B
reg RMS_lces_price_index_win inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter RMS_lces_price_index_win inst_linageres [aw=sqrt(RMS_avg_spending)], nq(100) ///
xtitle("Instrument for Annualized Growth of Market Size (%)") ylabel(,angle(0)) ///
ytitle ("Average CES Inflation" "for Continued Products, 2006-2015 (%)") ///
graphregion(color(white)) note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure5b.pdf", as(pdf) replace
graph save "$resultspath/Figure5b.gph", replace 
 
* Panel C
reg RMS_lfullces_pi_win_common inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter RMS_lfullces_pi_win_common inst_linageres [aw=sqrt(RMS_avg_spending)], nq(100) ///
xtitle("Instrument for Annualized Growth of Market Size (%)") ylabel(,angle(0)) ///
ytitle ("Average CES Inflation with" "Changes in Product Variety, 2006-2015 (%)") ///
graphregion(color(white)) note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white))
graph export "$resultspath/Figure5c.pdf", as(pdf) replace
graph save "$resultspath/Figure5c.gph", replace 
 
